layui.define(['table', 'form'], function (exports) {
    var table = layui.table;
    var form = layui.form;
    var listView = {
        refreshTable: function () {
            table.reload(id, {
                where: data.field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        },
        /*
        * 单一表头
        * */
        tableInit: function (options) {
            var pageSize = typeof (options.pageSz) == "undefined" || options.pageSz == '' ? 10 : options.pageSz;
            var id = options.id;
            var cols = options.cols;
            var where = options.where;
            var v_page = true;
            var done_back = options.done_back;
            var height = options.height;
            if (height == "" || height == null || height == undefined) {
                // height = 'full-200';
                height = 'full-115';
            }
            if (!options.page && typeof (options.page) != "undefined") {
                v_page = options.page;
            }

            for (var i = 0; i < cols.length; i++) {
                var col = cols[i];
                col.align = col.align ? col.align : "center";
            }
            table.render({
                elem: '#' + id,
                id: id,
                height: height,
                where: where,
                url: options.url,
                page: v_page,
                limit: pageSize,
                limits: pageSize == 10 ? [10, 20, 30, 40, 50, 60, 70, 80, 90] : [10, 20, 50, 100, 500, pageSize],
                cols: [cols],
                request: {
                    pageName: 'pageNo',
                    limitName: 'pageSz' // 每页数据量的参数名，默认：limit
                },
                response: {
                    // statusName: 'responseCode', // 数据状态的字段名称，默认：code
                    statusCode: '200', // 成功的状态码，默认：0
                    // msgName: 'responseMsg', // 状态信息的字段名称，默认：msg
                    // countName: 'result.total', // 数据总数的字段名称，默认：report
                    // dataName: 'result.list',
                    // countDto: 'result.countDto' //统计信息
                },
                parseData: function (res) { //res 即为原始返回的数据
                    var t = 0, d = [], c = 0, m = res.message;
                    if (res.data != undefined
                        && res.data != null
                        && res.data != '') {
                        if (options.page && typeof (options.page) != "undefined") {
                            t = res.data.total;
                        }
                        d = res.data.list;
                        c = res.countDto;
                    } else {
                        m = "暂无数据";
                    }
                    return {
                        "code": res.code, //解析接口状态
                        "msg": m, //解析提示文本
                        "count": t, //解析数据长度
                        "data": d, //解析数据列表
                        "countDto": c //统计信息
                    };
                },
                done: function (res, curr, count) {
                    if (typeof (done_back) == "function") {
                        done_back(res, curr, count);
                    }
                }
            });
            var toolEventListener = options.toolEventListener;
            if (toolEventListener) {
                table.on("tool(" + id + ")", function (obj) {
                    var layEvent = obj.event;
                    var method = toolEventListener[layEvent];
                    if (method) {
                        method.call(this, obj);
                    }
                });
            }

            form.on("submit(search)", function (data) {
                table.reload(id, {
                    where: data.field,
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });
        },
        /*
        * 复杂表头
        * */
        tableInit2: function (options) {
            var pageSize = 10;
            var id = options.id;
            var cols = options.cols;
            var where = options.where;
            var v_page = true;
            var done_back = options.done_back;
            if (!options.page && typeof (options.page) != "undefined") {
                v_page = options.page;
            }

            for (var i = 0; i < cols.length; i++) {
                var col = cols[i];
                for (var j = 0; j < col.length; j++) {
                    var c = col[j];
                    c.align = c.align ? c.align : "center";
                }
            }
            table.render({
                elem: '#' + id,
                id: id,
                where: where,
                url: options.url,
                page: v_page,
                cols: cols,
                request: {
                    pageName: 'pageNo',
                    limitName: 'pageSz' // 每页数据量的参数名，默认：limit
                },
                response: {
                    statusName: 'responseCode', // 数据状态的字段名称，默认：code
                    statusCode: '200', // 成功的状态码，默认：0
                    msgName: 'responseMsg', // 状态信息的字段名称，默认：msg
                    countName: 'data.total', // 数据总数的字段名称，默认：report
                    dataName: 'data.list',
                    countDto: 'data.countDto' //统计信息
                },
                done: function (res, curr, count) {
                    if (typeof (done_back) == "function") {
                        done_back(res, curr, count);
                    }
                }
            });
            var toolEventListener = options.toolEventListener;
            if (toolEventListener) {
                table.on("tool(" + id + ")", function (obj) {
                    var layEvent = obj.event;
                    var method = toolEventListener[layEvent];
                    if (method) {
                        method.call(this, obj);
                    }
                });
            }

            form.on("submit(search)", function (data) {
                table.reload(id, {
                    where: data.field,
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });
        },
        getTable: function () {
            return table;
        }
    };

    exports('listView', listView);
});
